package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //构造购物车对象
        //1. 判断添加的商品数据是否已经在购物车中存在--根据条件查询数据库
        //条件：用户id+商品id  ---(只能查自己名下的购物车)
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);

        //2. 如果查出来不存在，代表是新商品，需要构造购物车对象数据
        if (cart == null) {
            //2.1 如果是添加的菜品，需要查询菜品数据，获取菜品名称、图片、价格
            if (shoppingCartDTO.getDishId() != null) {
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
            }else {
                //2.2 如果是添加的套餐，需要查询套餐数据，获取套餐名称、图片、价格
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
            }
            //2.3 设置商品数量为1
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            //2.4 调用mapper的新增方法，将购物车数据存到shopping_cart表中
            shoppingCartMapper.insert(shoppingCart);
        }else {
            //3. 如果查出来数据，代表不是新商品，需要修改该商品的数量（数量加1即可）--update操作
            cart.setNumber(cart.getNumber() + 1);
            //3.1 调用mapper的更新方法，修改数量
            shoppingCartMapper.update(cart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //1.获取当前登录用户id
        Long userId = BaseContext.getCurrentId();

        //2.调用mapper查询方法，根据user_id查询，返回查询结果
        return shoppingCartMapper.list(userId);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        //1.获取当前登录用户id
        Long userId = BaseContext.getCurrentId();

        //2.调用mapper删除方法，根据user_id删除
        shoppingCartMapper.delete(userId);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);

        if(cart != null){
            Integer number = cart.getNumber();
            if(number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(cart.getId());
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.update(cart);
            }
        }
    }
}
